import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  // 1.state 存储全局变量的数据
  state: {
    city: '山东',
    userId: 100188
  },
  // getter
  getters: {
    // 获取state中的city
    getCity (state) {
      return state.city
    },
    getUserId (state) {
      return state.userId
    }
  },
  // action,通过action调用mutation中的方法
  actions: {
    setCityName (state, name) {
      state.commit('setCity', name)
    }
  },
  // 定义设置state的方法
  mutations: {
    setCity (state, name) {
      state.city = name
    }
  }
})

export default store
